<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> 收藏夹</title>
    <link rel="stylesheet" href="../js/element-ui/lib/theme-chalk/index.css">
    <style>
        /* 全局样式 */
        body {
            margin: 0;
            padding: 0;
            font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
            background-color: #f5f7fa;
            color: #333;
        }

        /* 顶部导航栏 */
        .header {
            background-color: white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logo {
            font-size: 24px;
            font-weight: bold;
            color: #1E90FF;
        }

        .nav {
            display: flex;
            align-items: center;
        }

        .nav-item {
            margin: 0 15px;
            cursor: pointer;
        }

        .nav-item.active {
            color: #1E90FF;
            font-weight: bold;
        }

        .search-box {
            display: flex;
            margin-left: 20px;
        }

        /* 主体布局 */
        .main-container {
            display: flex;
            max-width: 1200px;
            margin: 20px auto;
        }

        /* 右侧内容区 */
        .content {
            flex: 1;
            background-color: white;
            border-radius: 4px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }

        /* 商品列表 - 横向布局 */
        .product-list-horizontal {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .product-card-horizontal {
            display: flex;
            border: 1px solid #eee;
            border-radius: 4px;
            padding: 15px;
            transition: all 0.3s;
        }

        .product-card-horizontal:hover {
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        }

        .product-image-container-horizontal {
            width: 180px;
            height: 180px;
            flex-shrink: 0;
            overflow: hidden;
            margin-right: 20px;
        }

        .product-image-horizontal {
            width: 100%;
            height: 100%;
            object-fit: contain;
            transition: transform 0.3s;
        }

        .product-card-horizontal:hover .product-image-horizontal {
            transform: scale(1.05);
        }

        .product-content-horizontal {
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .product-name-horizontal {
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .product-info-row {
            display: flex;
            margin-bottom: 8px;
        }

        .product-info-label {
            width: 80px;
            color: #999;
            font-size: 14px;
        }

        .product-info-value {
            flex: 1;
            font-size: 14px;
        }

        .product-price-horizontal {
            color: #F56C6C;
            font-size: 20px;
            font-weight: bold;
            margin: 10px 0;
        }

        .product-actions-horizontal {
            margin-top: auto;
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        /* 按钮样式 */
        .buy-btn {
            background-color: #1E90FF;
            color: white;
        }

        .cart-btn {
            background-color: #f0f0f0;
            color: #333;
        }

        .remove-btn {
            color: #F56C6C;
            border-color: #F56C6C;
        }

        /* 批量操作区 */
        .batch-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .search-container {
            display: flex;
            align-items: center;
        }

        /* 加载状态 */
        .loading-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 200px;
        }

        .empty-state {
            text-align: center;
            padding: 40px 0;
            color: #999;
        }
    </style>
</head>

<body>
<div id="app">
    <!-- 顶部导航栏 -->
    <div class="header">
        <div class="logo">Hongyun</div>
        <div class="nav">
            <div class="search-box">
                <el-input placeholder="搜索" clearable style="width: 200px;"></el-input>
                <el-button type="primary" icon="el-icon-search"></el-button>
            </div>
        </div>
    </div>

    <!-- 主体内容 -->
    <div class="main-container">
        <!-- 右侧内容区 -->
        <div class="content">


            <!-- 加载状态 -->
            <div class="loading-container" v-if="loading">
                <el-icon class="is-loading" style="font-size: 24px;">
                    <i class="el-icon-loading"></i>
                </el-icon>
                <span style="margin-left: 10px;">加载中...</span>
            </div>

            <!-- 空状态 -->
            <div class="empty-state" v-else-if="products.length === 0">
                <el-icon style="font-size: 48px; color: #ccc;">
                    <i class="el-icon-folder-opened"></i>
                </el-icon>
                <p style="margin-top: 15px;">暂无收藏商品</p>
            </div>

            <!-- 商品列表 - 横向 -->
            <div class="product-list-horizontal" v-else>
                <!-- 商品卡片 -->
                <div class="product-card-horizontal" v-for="product in displayedProducts" :key="product.id">
                    <div class="product-image-container-horizontal">
                        <img
                                :src="product.imageUrl || '/default-product.jpg'"
                                alt="商品图片"
                                class="product-image-horizontal"
                                @error="handleImageError"
                        >
                    </div>

                    <div class="product-content-horizontal">
                        <div class="product-name-horizontal">{{ product.name || `商品名称` }}</div>

                        <div class="product-info-row">
                            <div class="product-info-label">适用机型：</div>
                            <div class="product-info-value">{{ product.model || '通用' }}</div>
                        </div>


                        <div class="product-info-row">
                            <div class="product-info-label">物料代码：</div>
                            <div class="product-info-value">{{ product.code || '无' }}</div>
                        </div>

                        <div class="product-info-row">
                            <div class="product-info-label">重量：</div>
                            <div class="product-info-value">{{ product.weight || '0' }}KG</div>
                        </div>

                        <div class="product-info-row">
                            <div class="product-info-label">销量：</div>
                            <div class="product-info-value">{{ product.sales || '0' }}件</div>
                        </div>

                        <div class="product-price-horizontal">￥{{ product.price || '0' }}</div>

                        <div class="product-actions-horizontal">
                            <el-button size="small" class="buy-btn" @click="buyNow(product)">立即购买</el-button>
                            <el-button size="small" class="cart-btn" @click="addToCart(product)">加入购物车</el-button>
                            <el-button size="small" class="remove-btn" @click="removeFavorite(product)">取消收藏</el-button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 分页 -->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="filteredProducts.length"
                    v-if="filteredProducts.length > 5"
                    style="margin-top: 20px;">
            </el-pagination>
        </div>
    </div>
</div>

<script src="../js/axios-0.18.0.js"></script>
<script src="../js/vue.js"></script>
<script src="../js/element-ui/lib/index.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                loading: true,
                products: [], // 从数据库获取的所有商品
                displayedProducts: [], // 当前显示的商品
                filteredProducts: [], // 过滤后的商品
                selectedProducts: [], // 选中的商品
                searchKeyword: '',
                currentPage: 1,
                pageSize: 10
            };
        },
        created() {
            this.fetchFavorites();
        },
        methods: {
            // 从数据库获取收藏商品
            fetchFavorites() {
                this.loading = true;
                axios.get('/product/selectProductFavorite').then(response => {
                    if (response.data.code === 20041) {
                        this.products = response.data.data;
                        this.filteredProducts = [...this.products];
                        this.updateDisplayedProducts();
                    } else {
                        this.$message.info(response.data.message || '收藏列表为空');
                    }
                }).catch(error => {
                    console.error('获取收藏列表出错:', error);
                    this.$message.error('获取收藏列表出错');
                }).finally(() => {
                    this.loading = false;
                });
            },

            //错误处理方法
            handleImageError(e) {
                e.target.src = '/default-product.jpg';
                e.target.onerror = null;
            },

            // 过滤商品
            filterFavorites() {
                if (!this.searchKeyword.trim()) {
                    this.filteredProducts = [...this.products];
                    this.updateDisplayedProducts();
                    return;
                }
                const keyword = this.searchKeyword.trim().toLowerCase();
                this.filteredProducts = this.products.filter(product =>
                    product.name.toLowerCase().includes(keyword) ||
                    (product.code && product.code.toLowerCase().includes(keyword))
                );

                this.currentPage = 1;
                this.updateDisplayedProducts();

                if (this.filteredProducts.length === 0) {
                    this.$message.warning('未找到匹配的商品');
                }
            },

            // 更新显示的商品(分页)
            updateDisplayedProducts() {
                const start = (this.currentPage - 1) * this.pageSize;
                const end = start + this.pageSize;
                this.displayedProducts = this.filteredProducts.slice(start, end);
            },

            // 立即购买
            buyNow(product) {

                // 跳转到结账页面并传递产品ID
                window.location.href = `checkout.html?productId=${product.id}`;

            },

            // 加入购物车
            addToCart(product) {
                const cartItem = {

                    userId:product.userId,
                    productId: product.productId,
                    quantity:1,

                };

                axios.post('/shoppingcarts/add', cartItem)
                    .then(response => {
                        if (response.data.code === 20011) {
                            this.$message.success('已加入购物车');
                        } else {
                            this.$message.error(response.data.message || '加入购物车失败');
                        }
                    })
                    .catch(error => {
                        console.error('加入购物车出错:', error);
                        this.$message.error('加入购物车出错');
                    });
            },

            // 取消收藏
            removeFavorite(product) {
                this.$confirm('确定要取消收藏该商品吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete(`/product/removeFavorite/${product.productId}`)
                        .then(response => {
                            if (response.data.code === 20021) {
                                this.$message.success('已取消收藏');
                                this.fetchFavorites(); // 刷新列表
                            } else {
                                this.$message.error(response.data.message || '取消收藏失败');
                            }
                        })
                        .catch(error => {
                            console.error('取消收藏出错:', error);
                            this.$message.error('取消收藏出错');
                        });
                }).catch(() => {
                    // 用户取消操作
                });
            },

            // 分页大小变化
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.updateDisplayedProducts();
            },

            // 当前页变化
            handleCurrentChange(newPage) {
                this.currentPage = newPage;
                this.updateDisplayedProducts();
            }
        }
    });
</script>
</body>
</html>